package com.zhu.library_management_system.service.Impl;

import com.zhu.library_management_system.pojo.BookBorrowing;
import com.zhu.library_management_system.repository.BookBorrowingRepository;
import com.zhu.library_management_system.repository.BookRepository;
import com.zhu.library_management_system.service.BookBorrowingService;
import com.zhu.library_management_system.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Service
public class BookBorrowingImpl implements BookBorrowingService {

    @Autowired
    BookBorrowingRepository bookBorrowingRepository;
    @Autowired
    BookService bookService;
    @Override
    public void borrowBook( BookBorrowing bookBorrowing) {
        bookBorrowingRepository.save(bookBorrowing);
    }

    @Override
    @Transactional
    public void returnBook(BookBorrowing bookBorrowing) {
        bookBorrowingRepository.realReturnDate(bookBorrowing.getId(),bookBorrowing.getRealReturnDate(),bookBorrowing.getBorrowStatus(),bookBorrowing.getOverdueFine());
    }

    @Override
    public LocalDateTime getReturnDate(Integer id) {
        return bookBorrowingRepository.getReturnDate(id);
    }

    @Override
    public Iterable<BookBorrowing> findBookBorrowingAll() {
        return bookBorrowingRepository.findAll();
    }

    @Override
    public BookBorrowing findBookBorrowingById(Integer id) {
        return bookBorrowingRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("借书记录不存在: " + id));
    }


    @Override
    public Integer countUserBorrowing(Integer userId) {
        return  bookBorrowingRepository.countUserBorrowing(userId);
    }

}
